﻿using Microsoft.Web.WebView2.WinForms;
using Microsoft.Web.WebView2.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
//install-package Microsoft.Web.WebView2 
namespace AutoXiGua
{
    public partial class Form1 : Form
    {
        string 话题;
        string 文案;
        public Form1()
        {
            InitializeComponent();
        }

      
        private async void Form1_Load(object sender, EventArgs e)
        {
            //webView2 = new WebView2();
            await webView2.EnsureCoreWebView2Async();

            //webView2.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = true;
            webView2.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
            webView2.Source = new Uri("https://xinghuo.xfyun.cn/");
        }

        private void CoreWebView2_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
        {

            webView2.ExecuteScriptAsync("document.documentElement.outerHTML").ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    string htmlContent = task.Result;
                    this.Invoke(new Action(() =>
                    {
                        this.toolStripStatusLabel_sysMsg.Text = webView2.Source.ToString();
                        this.textBox1.Text = Regex.Unescape( htmlContent);
                    }));


                    //MessageBox.Show(htmlContent); // 输出网页内容
                }
            });
        }
        #region 浏览器按钮
        private void toolStripButton_NavGo_Click(object sender, EventArgs e)
        {
            webView2.Source = new Uri("https://xinghuo.xfyun.cn/"); //webView2 打开地址，注意必须以http:// 或 https://开头 否则会报错
            
        }

        private void toolStripButton_GoBack_Click(object sender, EventArgs e)
        {
            webView2.GoBack();
           
        }

        private void toolStripButton_GoForward_Click(object sender, EventArgs e)
        {
            webView2.GoForward();
           
        }

        private void toolStripButton_GoHome_Click(object sender, EventArgs e)
        {
            webView2.Source = new Uri("https://www.baidu.com/");
        }

        private void toolStripButton_Refresh_Click(object sender, EventArgs e)
        {
            webView2.Refresh();
        }
        #endregion
        #region 西瓜视频自动生成发布脚本
        private void 获取题目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string 话题 = "新疆地震频发说说地震的形成原因";
            if (this.toolStripTextBox1.Text.Length==0)
                this.toolStripTextBox1.Text="生成一个新的科普话题";

            if (this.toolStripTextBox1.Text.Length > 5)
            {
                话题 = this.toolStripTextBox1.Text;
            }

            webView2.ExecuteScriptAsync($"document.getElementsByTagName('textarea')[1].value='科普一下，{话题},生成400字的文案'")                .ContinueWith(task =>
            {
            });
            webView2.ExecuteScriptAsync("document.getElementsByClassName('ask-window_send__xTavC')[0].click()").ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    string htmlContent = task.Result;
                    this.Invoke(new Action(() =>
                    {

                        //this.toolStripStatusLabel_sysMsg.Text = webView2.Source.ToString();
                        this.toolStripStatusLabel_sysMsg.Text = Regex.Unescape(htmlContent);
                        //this.textBox1.Text = Regex.Unescape(htmlContent);
                    }));


                    //MessageBox.Show("AA"); // 输出网页内容
                }
            });

        }

        private void 生成文本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(话题.Replace("科普话题：", ""));
            //webView2.ExecuteScriptAsync("document.getElementsByClassName(\"chat-window_feedback_icon__EhtQH\")[1].click()").ContinueWith(task =>
            //{
            //    if (!task.IsFaulted)
            //    {
            //        string htmlContent = task.Result;
            //        this.Invoke(new Action(() =>
            //        {
            //            //this.toolStripStatusLabel_sysMsg.Text = webView2.Source.ToString();
            //            this.toolStripStatusLabel_sysMsg.Text = Regex.Unescape(htmlContent);
            //            //this.textBox1.Text = Regex.Unescape(htmlContent);
            //        }));


            //        //MessageBox.Show("AA"); // 输出网页内容
            //    }
            //});
        }

        private void 生成视频ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            //markdown-body
            webView2.ExecuteScriptAsync("document.getElementsByClassName('markdown-body')[0].innerText").ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    string htmlContent = task.Result;
                    this.Invoke(new Action(() =>
                    {
                        string 文本 = Regex.Unescape(htmlContent);
                        话题 = 文本.Split('\n')[0].Replace("\"", "");
                        文案 = 文本.Substring(文本.IndexOf('\n'));
                        Clipboard.SetText(文案); 
                        //this.toolStripStatusLabel_sysMsg.Text = webView2.Source.ToString();
                        this.toolStripStatusLabel_sysMsg.Text = $"【{话题}】{文案.Replace("\n", "\\n").Substring(0, 50)}...";


                    }));


                    //MessageBox.Show("AA"); // 输出网页内容
                }
            });
        }

        private void 发布ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        #endregion

        private void 获取代码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webView2.ExecuteScriptAsync("document.documentElement.outerHTML").ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    string htmlContent = task.Result;
                    this.Invoke(new Action(() =>
                    {
                        this.toolStripStatusLabel_sysMsg.Text = webView2.Source.ToString();
                        this.textBox1.Text = Regex.Unescape(htmlContent);
                    }));

                }
            });
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string 话题 = "新疆地震频发说说地震的形成原因";
            if (this.toolStripTextBox1.Text.Length == 0)
                this.toolStripTextBox1.Text = "生成一个新的科普话题";

            if (this.toolStripTextBox1.Text.Length > 5)
            {
                话题 = this.toolStripTextBox1.Text;
            }

            webView2.ExecuteScriptAsync($"document.getElementsByTagName('textarea')[1].value='科普一下，{话题},生成400字的文案'").ContinueWith(task =>
            {
            });
            webView2.ExecuteScriptAsync("document.getElementsByClassName('ask-window_send__xTavC')[0].click()").ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    string htmlContent = task.Result;
                    this.Invoke(new Action(() =>
                    {

                        //this.toolStripStatusLabel_sysMsg.Text = webView2.Source.ToString();
                        this.toolStripStatusLabel_sysMsg.Text = Regex.Unescape(htmlContent);
                        //this.textBox1.Text = Regex.Unescape(htmlContent);
                    }));


                    //MessageBox.Show("AA"); // 输出网页内容
                }
            });
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            webView2.ExecuteScriptAsync("document.getElementsByClassName('markdown-body')[0].innerText").ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    string htmlContent = task.Result;
                    this.Invoke(new Action(() =>
                    {
                        string 文本 = Regex.Unescape(htmlContent);
                        话题 = 文本.Split('\n')[0].Replace("\"", "");
                        文案 = 文本.Substring(文本.IndexOf('\n'));
                        Clipboard.SetText(文案);
                        //this.toolStripStatusLabel_sysMsg.Text = webView2.Source.ToString();
                        this.toolStripStatusLabel_sysMsg.Text = $"【{话题}】{文案.Replace("\n", "\\n").Substring(0, 50)}...";


                    }));


                    //MessageBox.Show("AA"); // 输出网页内容
                }
            });
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(话题.Replace("科普话题：", "").Replace("话题：", ""));
        }
    }
}
